package com.glela.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.util.ParameterUtils;
import com.glela.seckill.model.RequestSecKillModel;
import com.glela.seckill.service.SeckillService;

@Controller
@RequestMapping(value = "/seckill")
public class SecKillController {
	
	private static Logger logger = LoggerFactory.getLogger(SecKillController.class);
	
	@Autowired
	private SeckillService seckillService;
	
	/**
	 * H5秒杀详情页
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/secKillDetail", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> secKillDetail(@RequestBody RequestSecKillModel model) throws Exception {
		try {
			if (ParameterUtils.validParams(model.getActivityId(),model.getType())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			if (model.getType() != 5) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29902), HttpStatus.OK);
			}
			return seckillService.seckillDetail(model.getActivityId());
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("获取H5秒杀详情页异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 首页秒杀信息
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/indexSeckill", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> indexSeckill(@RequestBody RequestSecKillModel model) throws Exception {
		try {
			if (ParameterUtils.validParams(model.getType())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			if (model.getType() != 5) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C29902), HttpStatus.OK);
			}
			return seckillService.indexSeckill();
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("获取首页秒杀数据异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 更多秒杀信息
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/moreSeckillInfo", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> moreSeckillInfo(@RequestBody RequestSecKillModel model) throws Exception {
		try {
			if (ParameterUtils.validParams(model.getPageNum(),model.getPageSize(),model.getStatus())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return seckillService.moreSeckillInfo(model.getStartIndex(),model.getPageSize(),model.getStatus());
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("获取更多秒杀信息异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	
	/**
	 * 秒杀库存判定
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/decideRepertory", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> decideRepertory(@RequestBody RequestSecKillModel model) throws Exception {
		try {
			if (ParameterUtils.validParams(model.getActivityId(), model.getGoodsId(), model.getSpecInfo())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return seckillService.decideRepertory(model.getActivityId(), model.getGoodsId(), model.getSpecInfo());
		} catch (Exception e) {
			// TODO: handle exception
			logger.error("获取更多秒杀信息异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
	

}
